<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="600px" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px">
			<el-form-item label="选择公文" prop="flowId">
				<el-select v-model="form.flowId" style="width: 100%;">
					<el-option v-for="item in flowList" :key="item.value" :value="item.value" :label="item.label"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="开始时间" prop="beginTime">
				<el-date-picker v-model="form.beginTime" placeholder="开始时间" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
			</el-form-item>
			<el-form-item label="结束时间" prop="endTime">
				<el-date-picker v-model="form.endTime" placeholder="结束时间" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
			</el-form-item>
			<el-form-item label="受托人" prop="toId">
				<select-user-dialog v-model:accountId="form.toId" :mode="false"/>
			</el-form-item>
			<el-form-item label="是否启用">
				<el-switch v-model="form.status" active-value="1" inactive-value="0"></el-switch>
			</el-form-item>
			<el-form-item label="备注" prop="remark">
				<el-input v-model="form.remark" maxlength="150" clearable type="textarea" rows="3"></el-input>
			</el-form-item>
			<msg-box-check-box v-model:msgType="form.msgType" code="doc-delegate"></msg-box-check-box>
		</el-form>
		<template #footer>
			<div style="text-align: center">
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
			</div>
		</template>
	</el-dialog>
</template>
<script>
import selectUserDialog from "@/views/module/selectUserDialog";
import msgBoxCheckBox from "@/views/module/msgBoxCheckBox";
export default {
	emits: ['success', 'closed'],
	components: {
		selectUserDialog,
		msgBoxCheckBox
	},
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增',
				edit: '编辑',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
				flowId:"",
				beginTime: "",
				endTime: "",
				toId:"",
				status:"1",
				model:'1',
				remark: "",
				msgType:"",
			},
			//验证规则
			rules: {
				flowId: [
					{required: true, message: '请选择委托的公文'}
				],
				beginTime: [
					{ required: true, message: '请选择开始时间'}
				],
				endTime: [
					{ required: true, message: '请选择结束时间'}
				],
			},
			flowList:[]
		}
	},
	mounted() {
		this.getFlowList();
	},
	methods: {
		//显示
		open(mode='add'){
			this.mode = mode;
			this.visible = true;
			return this
		},
		//加载树数据
		async getFlowList(){
			var res = await this.$API.bpm.bpmFlow.getMyBpmFlowListForSelect.get({model:'1'});
			this.flowList = res.data;
		},
		//表单提交方法
		submit(){
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if(this.mode=="add")
					{
						res = await this.$API.bpm.bpmDelegate.insertBpmDelegate.post(this.form);
					}else if(this.mode=="edit")
					{
						res = await this.$API.bpm.bpmDelegate.updateBpmDelegate.post(this.form);
					}
					this.isSaveing = false;
					if(res.code == 200){
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					}else{
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		//表单注入数据
		setData(data){
			this.form.delegateId = data.delegateId
			this.form.flowId = data.flowId
			this.form.beginTime = data.beginTime
			this.form.endTime = data.endTime
			this.form.toId = data.toId
			this.form.model = data.model
			this.form.status = data.status
			this.form.remark = data.remark
			//可以和上面一样单个注入，也可以像下面一样直接合并进去
			//Object.assign(this.form, data)
		},
	}
}
</script>

<style>
</style>
